package io;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;

public class FileTest {
    public static void main(String[] args) throws IOException {
        String filename = "src\\io\\FileTest.java";//定义文件路径
        BufferedReader br = new BufferedReader(new FileReader(filename));

        ArrayList<String> sz = new ArrayList<>();
        String s;
        while ((s=br.readLine())!=null){
            sz.add(s);
        }

        br.close();//关闭

        Collections.sort(sz,new cmp());//对字符串进行排序

        LinkedList<String> list = new LinkedList<>();
        list.addAll(sz);//将字符串数据存入到LinkedList

        //输出到文件
        File f = new File("src\\io\\out.txt");
        //判断文件是否存在
        if (!f.exists()){
            f.createNewFile();  //创建文件
        }

        DataOutputStream ds = new DataOutputStream(new FileOutputStream(f));
        Collections.reverse(list);
        for (String l:list){
            ds.writeBytes(l+"\n");
        }

        ds.close();
    }
}
class cmp implements Comparator<String>{

    @Override
    //对comparator进行重写
    public int compare(String s1, String s2) {
        s1 = s1.toUpperCase();
        s2 = s2.toUpperCase();

        return s2.compareTo(s1);
    }
}
